import requests
import json

code = {
    "sources": [
        {
            "name": "managed_jdbc",
            "config": {
                "resultTable": "source_table_datasource_type",
                "options": {
                    "connectionCollation": "utf8mb4_unicode_ci"
                },
                "datasource": "oceanbase_test",
                "query": "select * from xi_test_datasource_type"
            }
        },
        {
            "name": "managed_jdbc",
            "config": {
                "resultTable": "source_table_datasource_config_key",
                "options": {
                    "connectionCollation": "utf8mb4_unicode_ci"
                },
                "datasource": "oceanbase_test",
                "query": "select * from xi_test_datasource_config_key"
            }
        }
    ],
    "transformations": [
        {
            "name": "sql",
            "config": {
                "resultTable": "spark_transform_table_datasource_detail",
                "sql": """
select dt.type_code,
       dt.type_name,
       dt.icon,
       dt.classifier, 
       dck.key_code,
       dck.key_name,
       dck.key_order,
       dck.default_value,
       dck.value_type
from source_table_datasource_type dt
       join source_table_datasource_config_key dck on dt.type_code = dck.datasource_type
                """
            }
        }
    ],
    "sinks": [
        {
            "name": "file",
            "config": {
                "sourceTable": "spark_transform_table_datasource_detail",
                "path": "hdfs:///data/tmp_file_xishihao/${dt}",
                "serializer": "csv",
                "saveMode": "overwrite"
            }
        }
    ]
}

if __name__ == '__main__':
    url = "http://linkis-test:9001/api/rest_j/v1/entrance/submit"
    headers = {
        'Content-Type': 'application/json',
        'Token-Code': 'dss-AUTH',
        'Token-User': 'linkis'
    }

    requestBody ={
        "executionContent": {
            "code": json.dumps(code),
            "runType": "data_calc"
        },
        "params": {
            "variable": {
                "dt": "2022-01-01"
            },
            "configuration": {
                "startup": {
                    "spark.executor.memory": "4g",
                    "spark.driver.memory": "2g",
                    "spark.executor.cores": "1",
                    "spark.executor.instances": 1
                }
            }
        },
        "source": {
            "scriptPath": "path"
        },
        "labels": {
            "engineType": "spark-3.2.2",
            "executeOnce": "true",
            "userCreator": "linkis-IDE"
        }
    }
    res = requests.post(url=url, headers=headers, data=json.dumps(requestBody))
    print(res.text)